/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.mods.survival;

import com.jme3.math.Vector3f;
import etherblocks.engine.network.*;
import etherblocks.engine.network.messages.*;
import etherblocks.game.entitysystem.Entity;
import etherblocks.game.entitysystem.EntityTemplate;
import etherblocks.game.entitysystem.components.*;
import etherblocks.mods.survival.entitysystem.components.*;
import etherblocks.mods.survival.entitysystem.systems.*;
import etherblocks.mods.survival.messages.*;

/**
 *
 * @author Philipp
 */
public class SurvivalGameServer extends GameServer
{

    @Override
    public void onGameStarted(){
        addEntitySystem(new HungerSystem());
        addEntitySystem(new HealthSystem());
        addEntitySystem(new AggroSystem());
        addEntitySystem(new StareSystem());
        super.onGameStarted();
        for(int i = 0; i < 20; i++)
        {
            spawnAngryCow();
        }
    }

    @Override
    public MessageResponse getMessageResponse(int clientID, NetworkMessage receivedMessage){
        MessageResponse response = super.getMessageResponse(clientID, receivedMessage);
        Entity playerEntity = playerEntities.get(clientID);
        if(receivedMessage instanceof Message_HealCommand){
            HealthComponent health = playerEntity.getComponent(HealthComponent.class);
            playerEntity.setComponent(new HealthComponent(Integer.MAX_VALUE, health.getMaxHealth(), health.getRegeneration()));
        }
        else if(receivedMessage instanceof Message_EatCommand){
            HungerComponent hunger = playerEntity.getComponent(HungerComponent.class);
            playerEntity.setComponent(new HungerComponent(Integer.MAX_VALUE, hunger.getMaxHunger(), hunger.getRegeneration()));
        }
        else if(receivedMessage instanceof Message_ClientConnection)
        {
            playerEntity.setComponent(new HungerComponent(100, 100, -0.8f));
            playerEntity.setComponent(new HealthComponent(50, 100, 0.2f));
            playerEntity.setComponent(new CanBeAttackedComponent());
        }
        return response;
    }
    
    private void spawnAngryCow()
    {
        Entity angryCow = world.getEntityMap().createNewEntity();
        EntityTemplate.loadTemplate(angryCow, "cow");
        angryCow.setComponent(new AggroComponent(20));
        angryCow.setComponent(new LocationComponent(new Vector3f((float) Math.random() * 200, 50, (float) Math.random() * 200), System.currentTimeMillis()));
        angryCow.setComponent(new RotationComponent(new Vector3f(1, 0, 0), System.currentTimeMillis()));
    }
}
